import { searchRoute } from '@/utils/util';
import { useLocation } from 'react-router-dom';
import { routerArray } from '@/routers';
import { store } from '@/redux';
import { useEffect, useState } from 'react';
import { debounce } from 'lodash';

const useGetData = (GET_DATA: GetListApi<MineObject>, DEFAULT_FILTER: MineObject, LIST_NAME: String) => {
	const DEBOUNCE_TIME = 2000;
	const [page, setPageIndex] = useState(1);
	const [filterInfo, setFilterInfo] = useState(DEFAULT_FILTER);
	const [viewInfo, setViewInfo] = useState<GetListRes>({
		total: 10,
		list: [],
	});

	async function getData() {
		// setViewInfo({ total: 0, list: false })
		const pageInfo = { size: 15, page };
		const { total, list } = await GET_DATA({ ...filterInfo, ...pageInfo });
		setViewInfo({ list, total });
	}

	const debounce_get = debounce(() => {
		setPageIndex(1);
		getData();
	}, DEBOUNCE_TIME);

	useEffect(() => {
		getData();
	}, [page, filterInfo]);

	return {
		page,
		setPageIndex,
		filterInfo,
		setFilterInfo,
		viewInfo,
		setViewInfo,
		getData,
	};
};

export default useGetData;
